package com.scitc.service;

import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Service()
public class MatchService {

    private String path = System.getProperty("user.dir")
            + "\\src\\main\\resources\\Address\\Main.py";

    public String matchOnce(String addrName) {
        //调用python接口
        StringBuilder result = new StringBuilder();
        String[] args2 = new String[]{"python", path, "2", addrName};
        // 这个地方
        try {
            Process proc = Runtime.getRuntime().exec(args2);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "gbk"));
            String line = null;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return String.valueOf(result);
    }

    public String matchFile(String fileName) {
        StringBuilder result = new StringBuilder();
        String[] args2 = new String[]{"python", path, "3", fileName};
        try {
            Process proc = Runtime.getRuntime().exec(args2);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "gbk"));
            String line = null;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return String.valueOf(result);
    }

    public String train(String fileName) {
        StringBuilder result = new StringBuilder();
        String[] args2 = new String[]{"python", path, "1", fileName};
        try {
            Process proc = Runtime.getRuntime().exec(args2);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "gbk"));
            String line = null;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return String.valueOf(result);
    }

}